package  
{
	import flash.display.JointStyle;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.text.TextField;
	/**
	 * ...
	 * @author patsup
	 */
	public class LoadMenu extends Menu 
	{
		//menu image parameters
		private const COLOR:int = 0x00FF00;
		private const WIDTH:int = 640;
		private const HEIGHT:int = 480;
		
		private const MENU_BUTTON_X_STARTLOC:int = 400;			//Button for return back to menu
		private const MENU_BUTTON_Y_STARTLOC:int = 350;
		private const LOADGAME_1_BUTTON_X_STARTLOC:int = 50;	//Button for load game 1
		private const LOADGAME_1_BUTTON_Y_STARTLOC:int = 50;
		private const LOADGAME_2_BUTTON_X_STARTLOC:int = 250;	//Button for load game 2
		private const LOADGAME_2_BUTTON_Y_STARTLOC:int = 50;
		private const LOADGAME_3_BUTTON_X_STARTLOC:int = 50;	//Button for load game 3
		private const LOADGAME_3_BUTTON_Y_STARTLOC:int = 150;
		private const LOADGAME_4_BUTTON_X_STARTLOC:int = 250;	//Button for load game 4
		private const LOADGAME_4_BUTTON_Y_STARTLOC:int = 150;
		private const DELETE_BUTTON_X_STARTLOC:int = 50;		//Button to delete data
		private const DELETE_BUTTON_Y_STARTLOC:int = 350;
		private const CONFIRM_DEL_BUTTON_X_STARTLOC:int = 100;	//Button to confirm delete
		private const CONFIRM_DEL_BUTTON_Y_STARTLOC:int = 300;
		private const CANCEL_DEL_BUTTON_X_STARTLOC:int = 240;	//Button to cancel delete
		private const CANCEL_DEL_BUTTON_Y_STARTLOC:int = 300;
		
		private var txtLabel:TextField;
		
		private var mainRef:Main;
		private var thisStage:Stage;
		
		private var menuButtonImgData:ImageData;
		private var load1ButtonImgData:ImageData;
		private var load2ButtonImgData:ImageData;
		private var load3ButtonImgData:ImageData;
		private var load4ButtonImgData:ImageData;
		private var deleteButtonImgData:ImageData;
		private var confirmDeleteButtonImgData:ImageData;
		private var cancelDeleteButtonImgData:ImageData;
		private var confirmDelImgData:Vector.<ImageData>;
		
		private var menuButton:MenuButton;						//button to return to start menu
		private var loadButtons:Vector.<MenuButton>;			//4 load buttons
		private var confirmDelButtons:Vector.<MenuButton>;		//4 buttons to confirm whether to delete data slot
		private var deleteButton:MenuButton;
		private var confirmDeleteButton:MenuButton;
		private var cancelDeleteButton:MenuButton;
		
		private var whichDataSlotToDelete:int;
		
		public function LoadMenu(mainRef:Main) 
		{
			txtLabel = new TextField();
			this.mainRef = mainRef;
			thisStage = mainRef.stage;
			
			loadButtons = new Vector.<MenuButton>;				//give the VECTOR of menu buttons memory
			confirmDelButtons = new Vector.<MenuButton>;
			confirmDelImgData = new Vector.<ImageData>;
			
			//these image data objects don't have "labels" because they get set in Initialize()
			load1ButtonImgData = new ImageData(LOADGAME_1_BUTTON_X_STARTLOC, LOADGAME_1_BUTTON_Y_STARTLOC, "");
			load2ButtonImgData = new ImageData(LOADGAME_2_BUTTON_X_STARTLOC, LOADGAME_2_BUTTON_Y_STARTLOC, "");
			load3ButtonImgData = new ImageData(LOADGAME_3_BUTTON_X_STARTLOC, LOADGAME_3_BUTTON_Y_STARTLOC, "");
			load4ButtonImgData = new ImageData(LOADGAME_4_BUTTON_X_STARTLOC, LOADGAME_4_BUTTON_Y_STARTLOC, "");
			confirmDelImgData[0] = new ImageData(LOADGAME_1_BUTTON_X_STARTLOC, LOADGAME_1_BUTTON_Y_STARTLOC, "Delete game 1?");
			confirmDelImgData[1] = new ImageData(LOADGAME_2_BUTTON_X_STARTLOC, LOADGAME_2_BUTTON_Y_STARTLOC, "Delete game 2?");
			confirmDelImgData[2] = new ImageData(LOADGAME_3_BUTTON_X_STARTLOC, LOADGAME_3_BUTTON_Y_STARTLOC, "Delete game 3?");
			confirmDelImgData[3] = new ImageData(LOADGAME_4_BUTTON_X_STARTLOC, LOADGAME_4_BUTTON_Y_STARTLOC, "Delete game 4?");
			deleteButtonImgData = new ImageData(DELETE_BUTTON_X_STARTLOC, DELETE_BUTTON_Y_STARTLOC, "Delete data");
			menuButtonImgData = new ImageData(MENU_BUTTON_X_STARTLOC, MENU_BUTTON_Y_STARTLOC, "Back");
			confirmDeleteButtonImgData = new ImageData(CONFIRM_DEL_BUTTON_X_STARTLOC, CONFIRM_DEL_BUTTON_Y_STARTLOC, "Confirm");
			cancelDeleteButtonImgData = new ImageData(CANCEL_DEL_BUTTON_X_STARTLOC, CANCEL_DEL_BUTTON_Y_STARTLOC, "Cancel");
			
			loadButtons[0] = new MenuButton(load1ButtonImgData, LoadGame1BtnInputMouseClick);
			loadButtons[1] = new MenuButton(load2ButtonImgData, LoadGame2BtnInputMouseClick);
			loadButtons[2] = new MenuButton(load3ButtonImgData, LoadGame3BtnInputMouseClick);
			loadButtons[3] = new MenuButton(load4ButtonImgData, LoadGame4BtnInputMouseClick);
			confirmDelButtons[0] = new MenuButton(confirmDelImgData[0], DeleteGame1BtnInputMouseClick);
			confirmDelButtons[1] = new MenuButton(confirmDelImgData[1], DeleteGame2BtnInputMouseClick);
			confirmDelButtons[2] = new MenuButton(confirmDelImgData[2], DeleteGame3BtnInputMouseClick);
			confirmDelButtons[3] = new MenuButton(confirmDelImgData[3], DeleteGame4BtnInputMouseClick);
			deleteButton = new MenuButton(deleteButtonImgData, DeleteBtnInputMouseClick);
			menuButton = new MenuButton(menuButtonImgData, MenuBtnInputMouseClick);
			
			confirmDeleteButton = new MenuButton(confirmDeleteButtonImgData, ConfirmDeleteBtnInputMouseClick);
			cancelDeleteButton = new MenuButton(cancelDeleteButtonImgData, CancelDeleteBtnInputMouseClick);
			
			
			
			for (var i:int = 0; i < 4; i++) {
				addChild(loadButtons[i]);
			}
			addChild(deleteButton);
			addChild(menuButton);
			
			graphics.beginFill(COLOR);							//draw the background
			graphics.drawRect(0, 0, WIDTH, HEIGHT);
			graphics.endFill();
			
		}
		
		//Initialize()
		//initializes the Load menu... make sure to call it to refresh recently saved data
		public function Initialize():void
		{
			var _btnLabel:String;
			var _savedData:SharedObject;
			for (var i:int = 1; i <= 4; i++)
			{
				_savedData = SharedObject.getLocal("PirateGameSaveData" + i);
				if (_savedData.data.hasData == true) {
					_btnLabel = "Slot " + i + ": $" + _savedData.data.totalMoney + "\n" + _savedData.data.timeStamp;
				} else {
					_btnLabel = "New Game";
				}
				loadButtons[i - 1].SetButtonLabel(_btnLabel);
			}
			InitImage();
		}
		//InitImage()
		//Initializes the image of the Menu
		protected override function InitImage():void
		{
			txtLabel.text = "Load Menu";
			addChild(txtLabel);
			
			super.InitImage();
		}
		
		//DisableAllButtonsWhileConfirmingDelete()
		//disables all the buttons except the confirm/cancel-delete buttons
		private function DisableAllButtonsWhileConfirmingDelete():void
		{
			for (var i:int = 0; i < 4; i++) {
				loadButtons[i].DisableButton();
				confirmDelButtons[i].DisableButton();
			}
			deleteButton.DisableButton();
			menuButton.DisableButton();
		}
		private function ReEnableAllButtons():void
		{
			for (var i:int = 0; i < 4; i++) {
				loadButtons[i].EnableButton();
				confirmDelButtons[i].EnableButton();
			}
			deleteButton.EnableButton();
			menuButton.EnableButton();
		}
		
		//MenuBtnInputMouseClick()
		//called on back menu button mouse click
		protected function MenuBtnInputMouseClick():void
		{
			mainRef.ReturnToStartMenu(this);
		}
		protected function LoadGame1BtnInputMouseClick():void {
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData1"));
		}
		protected function LoadGame2BtnInputMouseClick():void {
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData2"));
		}
		protected function LoadGame3BtnInputMouseClick():void {
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData3"));
		}
		protected function LoadGame4BtnInputMouseClick():void {
			mainRef.LoadGame(this, SharedObject.getLocal("PirateGameSaveData4"));
		}
		
		//ConfirmDeleteBtnInputMouseClick()
		//delete the selected load-data, and do the same actions that cancel would do
		protected function ConfirmDeleteBtnInputMouseClick():void {
			mainRef.DeleteGame(whichDataSlotToDelete);
			CancelDeleteBtnInputMouseClick();
		}
		//CancelDeleteBtnInputMouseClick()
		//remove confirm/cancel buttons,
		//then do all the normal things
		protected function CancelDeleteBtnInputMouseClick():void {
			removeChild(confirmDeleteButton);
			removeChild(cancelDeleteButton);
			CancelDeleteBtnInputMouseClick2();
		}
		//CancelDeleteBtnInputMouseClick2()
		//change back load buttons, 
		//re-enable the buttons that were disabled, and change the deleteButton's click-function back
		private function CancelDeleteBtnInputMouseClick2():void {
			for (var i:int = 0; i < 4; i++) {
				removeChild(confirmDelButtons[i]);
			}
			ReEnableAllButtons();
			deleteButton.ChangeIMCFunc(DeleteBtnInputMouseClick);
		}
		
		//DeleteBtnInputMouseClick()
		//gets passed into MenuButton class for deleteButton
		//re-clicking it will cancel the delete action
		protected function DeleteBtnInputMouseClick():void {
			for (var i:int = 0; i < 4; i++) {
				addChild(confirmDelButtons[i]);				//overlay 4 new "delete game" buttons
			}
			deleteButton.ChangeIMCFunc(CancelDeleteBtnInputMouseClick2);
		}
		
		//DeleteGame1BtnInputMouseClick()
		//store which data slot was selected as potential delete candidate,
		//display the confirm/cancel delete buttons, and disable all the rest
		protected function DeleteGame1BtnInputMouseClick():void {
			whichDataSlotToDelete = 1;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
			DisableAllButtonsWhileConfirmingDelete();
		}
		protected function DeleteGame2BtnInputMouseClick():void {
			whichDataSlotToDelete = 2;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
		}
		protected function DeleteGame3BtnInputMouseClick():void {
			whichDataSlotToDelete = 3;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
		}
		protected function DeleteGame4BtnInputMouseClick():void {
			whichDataSlotToDelete = 4;
			addChild(confirmDeleteButton);
			addChild(cancelDeleteButton);
		}
		
		
	}

}